package com.yidu.qianfeng.config;

import com.yidu.qianfeng.service.impl.QfFailedHandler;
import com.yidu.qianfeng.service.impl.QfLogoutSuccessHandler;
import com.yidu.qianfeng.service.impl.QfSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;

/**
 * @Author: 匡兆杰
 * @Description: security安全框架配置类
 * @Date Created in 8:38 2021/3/3
 */

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Resource(name = "userDetailsService")
    private UserDetailsService userDetailsService;

    @Autowired
    private QfFailedHandler failedHandler;//登录失败逻辑处理

    @Autowired
    private QfSuccessHandler successHandler;//登录成功逻辑处理


    @Autowired
    private QfLogoutSuccessHandler logoutSuccessHandler;//登出成功逻辑处理

    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/500").permitAll()//500页面
                .antMatchers("/403").permitAll()//403页面
                .antMatchers("/404").permitAll()//404页面
                .antMatchers("/toLogin").permitAll()//登录页面
                /*.anyRequest().permitAll()*/
                .antMatchers("/resources/static/**").permitAll()
                .antMatchers("/","/index").authenticated()//首页

                .antMatchers("/goAddcheck").hasAnyAuthority("装车扫码","运单核对")
                .antMatchers("/goAddtrucksequence").hasAnyAuthority("装车序列","运单核对")
                .antMatchers("/goAddunloadscan").hasAnyAuthority("卸货扫码","运单核对")
                .antMatchers("/goAddunloadsequence").hasAnyAuthority("卸货序列","运单核对")
                .antMatchers("/goAdddeliveryscan").hasAnyAuthority("派送扫码","运单核对")
                .antMatchers("/goAdddeliverylist").hasAnyAuthority("派送序列","运单核对")
                .antMatchers("/goAdddsigner").hasAnyAuthority("签收扫码","运单核对")
                .antMatchers("/goAdddeliverylist").hasAnyAuthority("签收序列","运单核对")


                .antMatchers("/goAddwaybill").hasAnyAuthority("新增运单","运单管理")
                .antMatchers("/waybillList").hasAnyAuthority("运单列表","运单管理")

                .antMatchers("/addLoadingstowage").hasAnyAuthority("装车配货","运输管理")
                .antMatchers("/depart").hasAnyAuthority("发车列表","运输管理")

                .antMatchers("/goVehicleList").hasAnyAuthority("车辆列表","车辆管理")


                .antMatchers("/goShipper").hasAnyAuthority("发货方列表","客户管理")
                .antMatchers("/goConsignee").hasAnyAuthority("收货方列表","客户管理")

                .antMatchers("/employee").hasAnyAuthority("员工管理","公司管理")
                .antMatchers("/subAll").hasAnyAuthority("分站管理","公司管理")


                .antMatchers("/goAddTrack").hasAnyAuthority("新增跟踪","跟踪管理")


                .antMatchers("/income").hasAnyAuthority("收入明细","开支明细","收支明细","财务结算")

                .antMatchers("/goAddfinance").hasAnyAuthority("财务损益","指标考核")
                .antMatchers("/goAddwaybilltarget").hasAnyAuthority("运单指标","指标考核")

                .and()
                .exceptionHandling().accessDeniedPage("/noauth")
                .and()
                .formLogin().loginPage("/toLogin").loginProcessingUrl("/login")
                .usernameParameter("username")
                .passwordParameter("password")
                .failureHandler(failedHandler)//设置自定义逻辑处理登录失败
                .successHandler(successHandler)//设置自定义逻辑处理登录成功
                .and()
                .rememberMe()
                .rememberMeParameter("remember")
                .tokenValiditySeconds(60*60*60)
                .and()
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessHandler(logoutSuccessHandler)
                .and()
                .csrf().disable()
                .headers().frameOptions().disable();
    }


    //认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(new BCryptPasswordEncoder());
    }

    //编码加密
    @Bean
    PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

}
